package com.sia.springcloud.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sia.springcloud.controller.IDeviceController;
import com.sia.springcloud.model.Device;
import com.sia.springcloud.protocol.model.BoschDevice;
import com.sia.springcloud.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 通信协议设备表 前端控制器
 * </p>
 *
 * @author Sirius
 * @since 2021-12-17
 *
 */
@Slf4j
@RestController
public class DeviceController implements IDeviceController {

    @Resource
    private IDeviceService deviceService;


    /**
     * 添加设备
     *
     * @param request
     * @return
     */
    public String insertDevice(HttpServletRequest request) {
        Device device = packingDevice(request);
        return deviceService.insertDevice(device);
    }


    /**
     * 分页查询设备
     *
     * @param request
     * @return
     */
    public String queryDevice(HttpServletRequest request) {
        int start = Integer.parseInt(request.getParameter("start"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String protocolName = request.getParameter("protocolName");


        return deviceService.queryDevice(start, limit, protocolName);
    }

    /**
     * 删除设备
     *
     * @param request
     * @return
     */
    public String deleteDevice(HttpServletRequest request) {
        String[] deviceIdList = request.getParameter("id").split(",");

        return deviceService.deleteDevice(deviceIdList);
    }


    /**
     * 修改设备
     *
     * @param request
     * @return
     */
    public String updateDevice(HttpServletRequest request) {
        Device device = packingDevice(request);

        return deviceService.updateDevice(device);
    }

    @Override
    public String tableStructure() {
        return deviceService.tableStructure();
    }


    /**
     * 将请求参数包装到一个Device对象中并返回
     *
     * @param request
     * @return
     */
    private Device packingDevice(HttpServletRequest request) {
        String id = request.getParameter("id");
        String collect = request.getParameter("collect");
        String microservice = request.getParameter("microservice");
        String protocolName = request.getParameter("protocolName");
        String deviceName = request.getParameter("deviceName");
        String description = request.getParameter("description");
        String host = request.getParameter("host");
        String port = request.getParameter("port");
        String timeout = request.getParameter("timeout");
        String connectionLimit = request.getParameter("connectionLimit");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String dbName = request.getParameter("dbName");
        String filePath = request.getParameter("filePath");

        BoschDevice boschDevice = new BoschDevice();
        if (StringUtils.isNotEmpty(id)) {
            boschDevice.setId(Integer.parseInt(id));
        }
        if (StringUtils.isNotEmpty(collect)) {
            boschDevice.setCollect(Integer.parseInt(collect));
        }
        if (StringUtils.isNotEmpty(microservice)) {
            boschDevice.setMicroservice(microservice);
        }
        if (StringUtils.isNotEmpty(protocolName)) {
            boschDevice.setProtocolName(protocolName);
        }
        if (StringUtils.isNotEmpty(deviceName)) {
            boschDevice.setDeviceName(deviceName);
        }
        if (StringUtils.isNotEmpty(description)) {
            boschDevice.setDescription(description);
        }
        if (StringUtils.isNotEmpty(host)) {
            boschDevice.setHost(host);
        }
        if (StringUtils.isNotEmpty(port)) {
            boschDevice.setPort(Integer.parseInt(port));
        }
        if (StringUtils.isNotEmpty(timeout)) {
            boschDevice.setTimeout(Integer.parseInt(timeout));
        }
        if (StringUtils.isNotEmpty(connectionLimit)) {
            boschDevice.setConnectionLimit(Integer.parseInt(connectionLimit));
        }
        if (StringUtils.isNotEmpty(username)) {
            boschDevice.setUsername(username);
        }
        if (StringUtils.isNotEmpty(password)) {
            boschDevice.setPassword(password);
        }
        if (StringUtils.isNotEmpty(dbName)) {
            boschDevice.setDbName(dbName);
        }
        if (StringUtils.isNotEmpty(filePath)) {
            boschDevice.setFilePath(filePath);
        }

        Device device = BoschDevice.convert(boschDevice);

        return device;
    }

}
